﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    //张永畅 20240751189
    public class BookController : ApiController
    {
        BookBO _bookBO = new BookBO();
        public IHttpActionResult GetBookInfo(int BookID)
        {
            try
            {
                var result = _bookBO.GetBookinfo(BookID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #region 删除单条记录
        [HttpPost]
        public IHttpActionResult DeleteBookInfo([FromBody] JObject obj)
        {
            if (obj == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.DeleteBookInfo(obj["BookID"].ToObject<int>());
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 修改单条记录
        ///<summary>
        ///书籍信息
        ///</summary>
        ///<param name="code"></param>
        ///<returns></returns>
        [HttpPost]
        public IHttpActionResult UpdateBookInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.UpdateBookInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }

        #endregion

        #region 添加信息
        ///<summary>
        ///书籍信息 保存单条纪录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult SaveBookInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _bookBO.SaveBookInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }

        #endregion

        #region 书籍信息全记录
        [HttpGet]
        public IHttpActionResult GetBookList()
        {
            try
            {
                var result = _bookBO.GetBookList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 书籍信息 分页列表
        /// <summary>
        /// 书籍信息 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetBookList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetBookListWhereC(model.condidate);
                var result = _bookBO.GetBookList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetBookListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.bookName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookNo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and BookNO like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}
